package com.zj.osm.view.project.projectdetail.fragment;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ToastUtils;
import com.zj.osm.App;
import com.zj.osm.R;
import com.zj.osm.entity.db.ProjectDao;
import com.zj.osm.entity.greendao.Project;
import com.zj.osm.model.pojo.Equip;
import com.zj.osm.model.pojo.EquipModuleType;
import com.zj.osm.model.pojo.EquipType;
import com.zj.osm.model.pojo.Module;
import com.zj.osm.model.pojo.ModuleType;
import com.zj.osm.utils.ExportUnit;
import com.zj.osm.utils.SQLiteDatabaseUtils;
import com.zj.osm.utils.ThreadManager;
import com.zj.osm.utils.db_utils.ModuleUtils;
import com.zj.osm.view.BaseFragment;
import com.zj.osm.view.project.projectdetail.ProjectDetailActivity;
import com.zj.osm.view.project.projectdetail.fragment.scheme.PowerSupplySchemeFragment;
import com.zj.utils.utils.SpinnerUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.zj.osm.utils.ThreadManager.getThreadPool;
import static com.zj.osm.view.project.projectdetail.ProjectDetailActivity.PROJECT_DETAILS_ID;
import static com.zj.utils.utils.SpinnerUtils.setSpinnerItemSelectedByValue;


/**
 * 本地項目詳情中的fragment
 */
public class BasisFragment extends Fragment implements View.OnClickListener {
    private Project items;
    private TextView ba_tv_name;
    private Spinner ba_tv_dengji;
    private Spinner ba_tv_leixing;
    private TextView ba_tv_gaizao;
    private List<Button> buttonList = new ArrayList<>();

    private TextView xuanxingCenterTv;
    private TextView jiakongTv;
    private TextView dianlanTv;
    private ArrayList<Module> gangaoArray = new ArrayList<>();
    private String ganxinLiebieTvString;
    private Spinner lineSpLaying;
    private String fusheFangshi = "";

    /**
     * 基础信息
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        assert getArguments() != null;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_basis, container, false);
        initView(view);
        /*assert getArguments() != null;
        cid = getArguments().getInt("DATA");*/
        return view;
    }

    private Button map_btn_cancel;
    private String OldName;

    private void initView(View view) {
        map_btn_cancel = view.findViewById(R.id.map_btn_cancel);
        map_btn_cancel.setOnClickListener(this);
        TextView add_tv_time = view.findViewById(R.id.add_tv_time);
        ba_tv_name = view.findViewById(R.id.ba_tv_name);
        ba_tv_dengji = view.findViewById(R.id.ba_sp_dengji);
        ba_tv_leixing = view.findViewById(R.id.ba_sp_leixing);
        ba_tv_gaizao = view.findViewById(R.id.ba_tv_gaizao);
        Button line_btn_save = view.findViewById(R.id.line_btn_save);
        line_btn_save.setOnClickListener(this);
        //选型部分---------------------------------------------------
        xuanxingCenterTv = view.findViewById(R.id.xuanxingCenterTv);
        jiakongTv = view.findViewById(R.id.jiakongTv);
        dianlanTv = view.findViewById(R.id.dianlanTv);
        items = App.getInstances().getDaoSession().getProjectDao().queryBuilder().where(ProjectDao.Properties.Id.eq(PROJECT_DETAILS_ID)).build().unique();
        if (items != null) {
            Log.e("items", items.toString());
            ba_tv_name.setText(items.getName());
            OldName = items.getName();
            add_tv_time.setText(items.getTime());
            setSpinnerItemSelectedByValue(ba_tv_dengji, items.getVoltage());
            ba_tv_gaizao.setText(items.getNote());
            setSpinnerItemSelectedByValue(ba_tv_leixing, items.getType());
            ///储存 杆型 架高线路  电缆线路   id,id|id,id|id,id  根据  |  截取 然后 id,id   id,id  id,id 然后根据  , 截图
            /// isT=1（是）其他 ""
            if (items.getGanxinModuleTypeId() != null&&!items.getGanxinModuleTypeId().isEmpty()&&!items.getGanxinModuleTypeId().equals("")) {//类别 ModuleTypeID
                ganxinLiebieStringID = items.getGanxinModuleTypeId();
            }
            if (items.getGanxinModuleId() != null&&!items.getGanxinModuleId().isEmpty()&&!items.getGanxinModuleId().equals("")) {//杆型ID[非预应力,整根杆，12m,190mm，N]
                ganxinLiebieTvID = items.getGanxinModuleId();
                xuanxingCenterTv.setText(getGangaoResultId(ganxinLiebieTvID));
            }
            if (items.getJiakongEquipId() != null&&!items.getJiakongEquipId().isEmpty()&&!items.getJiakongEquipId().equals("")) {//杆型ID[非预应力,整根杆，12m,190mm，N]
                jiakongStringID = items.getJiakongEquipId();
                jiakongStringTvID = items.getJiakongModuleId();
                jiakongTv.setText(getLuxianXuanxingName(items.getJiakongModuleId()));

            }
            if (items.getDilanEquipId() != null&&!items.getDilanEquipId().isEmpty()&&!items.getDilanEquipId().equals("")) {//杆型ID[非预应力,整根杆，12m,190mm，N]
                dianlanStringID = items.getDilanEquipId();
                dianlanStringTvID = items.getDilanModuleId();
                dianlanTv.setText(getLuxianXuanxingName(dianlanStringTvID));

            }
            if (items.getPath() != null) {
                fusheFangshi = items.getPath();
            }


        }
        ImageButton detailsBtnBack = view.findViewById(R.id.detailsBtnBack);
        detailsBtnBack.setOnClickListener(this);
        TextView detailsTvTwo = view.findViewById(R.id.detailsTvTwo);
        detailsTvTwo.setOnClickListener(this);
        TextView detailsTvOne = view.findViewById(R.id.detailsTvOne);
        detailsTvOne.setOnClickListener(this);
        Button detailsBtnJc = view.findViewById(R.id.detailsBtnJc);
        detailsBtnJc.setOnClickListener(this);
        Button detailsBtnKcxx = view.findViewById(R.id.detailsBtnKcxx);
        detailsBtnKcxx.setOnClickListener(this);
        Button detailsBtnWlqd = view.findViewById(R.id.detailsBtnWlqd);
        detailsBtnWlqd.setOnClickListener(this);
        Button detailsBtnGdfa = view.findViewById(R.id.detailsBtnGdfa);

        xuanxingCenterTv.setOnClickListener(this);
        jiakongTv.setOnClickListener(this);
        dianlanTv.setOnClickListener(this);


        detailsBtnGdfa.setOnClickListener(this);
        detailsBtnJc.setBackgroundResource(R.drawable.btn_shape);
        detailsBtnKcxx.setBackgroundResource(R.drawable.btn_shape_white);
        detailsBtnWlqd.setBackgroundResource(R.drawable.btn_shape_white);
        detailsBtnGdfa.setBackgroundResource(R.drawable.btn_shape_white);
        detailsTvTwo.setText("保存");
        detailsTvTwo.setVisibility(View.GONE);
        buttonList.clear();
        buttonList.add(detailsBtnJc);
        buttonList.add(detailsBtnKcxx);
        buttonList.add(detailsBtnWlqd);
        buttonList.add(detailsBtnGdfa);

        setButtonStyle(detailsBtnJc);
        clickItem(this, detailsBtnBack, detailsBtnJc, detailsBtnKcxx, detailsBtnWlqd, detailsBtnGdfa);
    }

    /**
     * 保存当前项目配置信息
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void save() {
        if (ba_tv_name.getText().toString().equals("")) {
            com.blankj.utilcode.util.ToastUtils.showShort("请输入项目名称");
        } else {
            //这里去跟新项目内容，没修改一次都进行
            items.setName(ba_tv_name.getText().toString());
            items.setVoltage(ba_tv_dengji.getSelectedItem().toString());
            items.setType(ba_tv_leixing.getSelectedItem().toString());
            items.setNote(ba_tv_gaizao.getText().toString() + "");
            items.setGanxinModuleTypeId(ganxinLiebieStringID);
            items.setGanxinModuleId(ganxinLiebieTvID);
            items.setJiakongModuleId(jiakongStringTvID);
            items.setJiakongEquipId(jiakongStringID);
            items.setDilanModuleId(dianlanStringTvID);
            items.setDilanEquipId(dianlanStringID);
            items.setPath(fusheFangshi);
            //修改文件夹名称和内部名称
            //reFileName(items.getName());
            pushToServer(items, items.getId());
            App.getInstances().getDaoSession().getProjectDao().update(items);
            ToastUtils.showShort("修改成功");
            //修改项目列别表包名称
        }

        //退出
        if (getActivity() != null)
            getActivity().finish();
    }

    private void reFileName(String name) {
        //想命名的原文件的路径
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bhne/" + OldName);
        if (file.exists()) {//存在就变
            //将原文件更改为f:\a\b.xlsx，其中路径是必要的。注意
            file.renameTo(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bhne/" + name));
        }
        File fileIntofile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bhne/" + name + "/" + OldName);
        if (fileIntofile.exists()) {//存在就变
            //将原文件更改为f:\a\b.xlsx，其中路径是必要的。注意
            fileIntofile.renameTo(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bhne/" + name + "/" + name));
        }

    }


    /**
     * 发送到服务端
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void pushToServer(Project items, long id) {
        ExportUnit exportUnit = new ExportUnit(id, items.getName());
        exportUnit.setAddItemExport();


    }

    /**
     * 根据不同的fragment跳转不同的fragment
     *
     * @param fragment       原fragment
     * @param detailsBtnBack 返回
     * @param detailsBtnJc   基础信息
     * @param detailsBtnKcxx 勘察信息
     * @param detailsBtnWlqd 物料清单
     * @param detailsBtnGdfa 地形点or供电方案
     */
    public void clickItem(Fragment fragment, ImageButton detailsBtnBack, Button detailsBtnJc, Button detailsBtnKcxx, Button detailsBtnWlqd, Button detailsBtnGdfa) {
        buttonList.clear();
        buttonList.add(detailsBtnJc);
        buttonList.add(detailsBtnKcxx);
        buttonList.add(detailsBtnWlqd);
        buttonList.add(detailsBtnGdfa);
        detailsBtnBack.setOnClickListener(v -> {
            if (fragment.getActivity() != null)
                fragment.getActivity().finish();
        });
        detailsBtnJc.setOnClickListener(v -> {
            changeFragment(fragment, new BasisFragment());
            setButtonStyle(detailsBtnJc);
        });
        detailsBtnKcxx.setOnClickListener(v -> {
            changeFragment(fragment, new PoleFragment());
            setButtonStyle(detailsBtnKcxx);
        });
        detailsBtnWlqd.setOnClickListener(v -> {
            changeFragment(fragment, new MaterialFragment());
            setButtonStyle(detailsBtnWlqd);
        });
        detailsBtnGdfa.setOnClickListener(v -> {
            //changeFragment(fragment, new CrossFragment());//地形点
            changeFragment(fragment, new PowerSupplySchemeFragment());//供电方案
            setButtonStyle(detailsBtnGdfa);
        });
    }

    /**
     * 设置按钮的样式
     *
     * @param button 需要设置样式的按钮
     */
    public void setButtonStyle(Button button) {
        if (buttonList.size() > 0) {
            for (int i = 0; i < buttonList.size(); i++) {
                if (buttonList.get(i).getText().equals(button.getText())) {
                    button.setBackgroundResource(R.drawable.btn_shape);
                    button.setTextColor(Color.rgb(255, 255, 255));
                } else {
                    buttonList.get(i).setBackgroundResource(R.drawable.btn_shape_white);
                    buttonList.get(i).setTextColor(Color.rgb(51, 51, 51));
                }
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onClick(View v) {
        //保存detailsTvTwo
        switch (v.getId()) {
            case R.id.line_btn_save:
                save();
                break;
            case R.id.xuanxingCenterTv://杆型
                showXuanxingDialog(1);
                break;
            case R.id.jiakongTv: //架空线路
                showXuanxingDialog(2);
                break;
            case R.id.dianlanTv://电缆
                showXuanxingDialog(3);
                break;
            case R.id.map_btn_cancel:
                if (getActivity() != null)
                    getActivity().finish();
                break;
        }
    }

    String jiakongStringID = "";
    String dianlanStringID = "";
    String ganxinLiebieStringID = "";
    String jiakongStringTvID = "";
    String dianlanStringTvID = "";
    String ganxinLiebieTvID = "";

    private void showXuanxingDialog(int flag) {
        final View views = View.inflate(getContext(), R.layout.activity_program_dialog, null);
        final Spinner map_ed_levers = views.findViewById(R.id.map_ed_lever);
        final Spinner map_ed_head = views.findViewById(R.id.map_ed_head);
        final Spinner map_ed_shaojing = views.findViewById(R.id.map_ed_shaojing);
        final Spinner map_ed_ganxing = views.findViewById(R.id.map_ed_ganxings);
        final Spinner map_sp_three = views.findViewById(R.id.map_sp_three);
        final LinearLayout gangao_line = views.findViewById(R.id.gangao_line);
        final TextView xinluLiebie = views.findViewById(R.id.xianluLiebbie);
        final TextView xinluLieXing = views.findViewById(R.id.xianluLieXing);
        RelativeLayout lineRlLaying = views.findViewById(R.id.lineRlLaying);
        lineSpLaying = views.findViewById(R.id.lineSpLaying);
        if (flag == 1) {
            gangao_line.setVisibility(View.VISIBLE);
        }
        TextView mapTvNum = views.findViewById(R.id.mapTvNum);
        Button map_btn_quxiao = views.findViewById(R.id.map_btn_quxiao);
        Button map_btn_queding = views.findViewById(R.id.map_btn_queding);
        TextView map_tv_title = views.findViewById(R.id.map_tv_title);

        AlertDialog dialogFushu = new AlertDialog.Builder(getContext()).create();

        map_btn_quxiao.setOnClickListener(v -> {
            dialogFushu.dismiss();

        });
        map_btn_queding.setOnClickListener(v -> {
            switch (flag) {
                case 1://杆型
                    ModuleType moduleType = (ModuleType) map_ed_levers.getSelectedItem();
                    Module module = (Module) map_ed_ganxing.getSelectedItem();
                    xuanxingCenterTv.setText(module.getModuleName());
                    Log.e("alllGanxin", moduleType.toStrings());
                    Log.e("idll==", module.toStrings());
                    ganxinLiebieStringID = moduleType.getModuleTypeID();
                    Log.e("====ID", module.getModuleID());
                    ganxinLiebieTvID = module.getModuleID();
                    Log.e("====", moduleType.getName());
                    ganxinLiebieTvString = moduleType.getName();
                    break;
                case 2:
                    Equip equip = (Equip) map_ed_levers.getSelectedItem();
                    Module module2 = (Module) map_ed_ganxing.getSelectedItem();
                    jiakongStringID = equip.getId() + "";
                    jiakongStringTvID = module2.getModuleID();
                    jiakongTv.setText(module2.toString());
                    break;
                case 3:
                    Equip equip3 = (Equip) map_ed_levers.getSelectedItem();
                    Module module3 = (Module) map_ed_ganxing.getSelectedItem();
                    dianlanStringID = equip3.getId() + "";

                    dianlanStringTvID = module3.getModuleID();
                    dianlanTv.setText(module3.toString());
                    fusheFangshi = lineSpLaying.getSelectedItem().toString();
                    break;
            }
            dialogFushu.dismiss();
        });


        switch (flag) {
            case 1://杆型
                lineRlLaying.setVisibility(View.GONE);
                //类型
                List<ModuleType> moduleTypeList = getLiebie();
                //适配器
                ArrayAdapter<ModuleType> arr_adapter21 = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, moduleTypeList);
                arr_adapter21.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                map_ed_levers.setAdapter(arr_adapter21);
                if (!ganxinLiebieStringID.isEmpty()) {
                    for (int i = 0; i < moduleTypeList.size(); i++) {
                        if (moduleTypeList.get(i).getModuleTypeID().contains(ganxinLiebieStringID)) {
                            map_ed_levers.setSelection(i);
                            break;
                        }
                    }
                }

                map_ed_levers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        ModuleType typeClass = moduleTypeList.get(position);
                        //获取杆高这些的类型。
                        gangaoArray = getGangaoResult(typeClass.getModuleTypeKey());
                        ///最终复制的内容。
                        ArrayAdapter<Module> ganxinAdapter = new ArrayAdapter<Module>(getContext(), android.R.layout.simple_spinner_item, gangaoArray);
                        ganxinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(ganxinAdapter);

                        for (int i = 0; i < gangaoArray.size(); i++) {
                            Log.e("111111====", ganxinLiebieTvID);
                            Log.e("111112====", gangaoArray.get(i).getModuleID());
                            if (gangaoArray.get(i).getModuleID().contains(ganxinLiebieTvID)) {
                                Log.e("zque", "通过" + i);
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                map_ed_head.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        ArrayList<Module> gangaoNew = new ArrayList<>();
                        Log.e("000", gangaoArray.toString());
                        for (Module moduleInser : gangaoArray) {
                            Log.e("allllllll", map_ed_head.getSelectedItem().toString() + "---" + map_ed_head.getSelectedItem().toString().equals("全部") + "---" + (moduleInser.getModuleName().contains(map_ed_head.getSelectedItem().toString()) || map_ed_head.getSelectedItem().toString().equals("全部")));

                            if ((moduleInser.getModuleName().contains(map_ed_head.getSelectedItem().toString()) || map_ed_head.getSelectedItem().toString().equals("全部"))
                                    && (moduleInser.getModuleName().contains(map_ed_shaojing.getSelectedItem().toString()) || map_ed_shaojing.getSelectedItem().toString().equals("全部"))) {
                                Log.e("alllll2", moduleInser.toString());
                                gangaoNew.add(moduleInser);
                            }
                        }
                        Log.e("allllllllllll", gangaoNew.toString());

                        ArrayAdapter<Module> ganxinAdapter = new ArrayAdapter<Module>(getContext(), android.R.layout.simple_spinner_item, gangaoNew);
                        ganxinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(ganxinAdapter);

                        for (int i = 0; i < gangaoNew.size(); i++) {
                            Log.e("111111====", ganxinLiebieTvID);
                            Log.e("111112====", gangaoArray.get(i).getModuleID());
                            if (gangaoNew.get(i).getModuleID().contains(ganxinLiebieTvID)) {
                                Log.e("zque", "通过" + i);
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

                map_ed_shaojing.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        ArrayList<Module> gangaoNew = new ArrayList<>();
                        for (Module moduleInser : gangaoArray) {
                            if ((moduleInser.getModuleName().contains(map_ed_head.getSelectedItem().toString()) || map_ed_head.getSelectedItem().toString().equals("全部"))
                                    && (moduleInser.getModuleName().contains(map_ed_shaojing.getSelectedItem().toString()) || map_ed_shaojing.getSelectedItem().toString().equals("全部"))) {
                                gangaoNew.add(moduleInser);
                            }
                        }

                        ArrayAdapter<Module> ganxinAdapter = new ArrayAdapter<Module>(getContext(), android.R.layout.simple_spinner_item, gangaoNew);
                        ganxinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(ganxinAdapter);

                        for (int i = 0; i < gangaoNew.size(); i++) {
                            Log.e("111111====", ganxinLiebieTvID);
                            Log.e("111112====", gangaoArray.get(i).getModuleID());
                            if (gangaoNew.get(i).getModuleID().contains(ganxinLiebieTvID)) {
                                Log.e("zque", "通过" + i);
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
            case 2: {
                lineRlLaying.setVisibility(View.GONE);
                xinluLiebie.setText("类别");
                xinluLieXing.setText("线路");
                List<Equip> equipList = getGuPiaoDai(3, "架空");
                //适配器
                ArrayAdapter<Equip> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, equipList);
                //设置样式
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                map_ed_levers.setAdapter(adapter);
                if (!jiakongStringID.isEmpty()) {
                    for (int i = 0; i < equipList.size(); i++) {
                        if ((equipList.get(i).getId() + "").contains(jiakongStringID)) {
                            map_ed_levers.setSelection(i);
                            break;
                        }
                    }
                }
                map_ed_levers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        Equip equip = ((Equip) map_ed_levers.getSelectedItem());
                        List<Module> modulesList = ModuleUtils.getInstance().getModuleList(equip.getId());
                        //适配器
                        ArrayAdapter<Module> arr_adapter4 = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, modulesList);
                        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(arr_adapter4);
                        for (int i = 0; i < modulesList.size(); i++) {
                            if (modulesList.get(i).getModuleID().contains(jiakongStringTvID)) {
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }

            break;
            case 3: {
                lineRlLaying.setVisibility(View.VISIBLE);
                String[] linePath = getResources().getStringArray(R.array.laying);
                for (int i = 0; i < linePath.length; i++) {
                    if (linePath[i].contains(fusheFangshi)) {
                        lineSpLaying.setSelection(i);
                        break;
                    }
                }
                xinluLiebie.setText("类别");
                xinluLieXing.setText("线路");
                List<Equip> equipList = getGuPiaoDai(3, "电缆");
                //适配器
                ArrayAdapter<Equip> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, equipList);
                //设置样式
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                map_ed_levers.setAdapter(adapter);
                if (!dianlanStringID.isEmpty()) {
                    for (int i = 0; i < equipList.size(); i++) {
                        if ((equipList.get(i).getId() + "").contains(dianlanStringID)) {
                            map_ed_levers.setSelection(i);
                            break;
                        }
                    }
                }
                map_ed_levers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        Equip equip = ((Equip) map_ed_levers.getSelectedItem());
                        List<Module> modulesList = ModuleUtils.getInstance().getModuleList(equip.getId());
                        //适配器
                        ArrayAdapter<Module> arr_adapter4 = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, modulesList);
                        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        map_ed_ganxing.setAdapter(arr_adapter4);
                        for (int i = 0; i < modulesList.size(); i++) {
                            if (modulesList.get(i).getModuleID().contains(dianlanStringTvID)) {
                                map_ed_ganxing.setSelection(i);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }
            break;
        }
        dialogFushu.setView(views);
        dialogFushu.show();

    }

    /**
     * 获取杆高这些信息
     *
     * @param
     */
    private String getGangaoResultId(String moduleId) {
        String moduleName = "设置值";
        SQLiteDatabase db = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        assert db != null;
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from Module m where m.ModuleID =" + "'" + moduleId + "'", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            moduleName = cursor.getString(1);
        }

        cursor.close();


        return moduleName;
    }

    private List<ModuleType> allMT = new ArrayList<>();

    /**
     * 获取杆高这些信息
     *
     * @param moduleTypeKey
     */
    private ArrayList<Module> getGangaoResult(String moduleTypeKey) {
        ArrayList<Module> module_array = new ArrayList<>();
        SQLiteDatabase db = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        Log.e("sql", "select * from Module m where m.ModuleTypeKey =" + moduleTypeKey);
        assert db != null;
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from Module m where m.ModuleTypeKey =" + "'" + moduleTypeKey + "'", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            Module module = new Module();
            module.setModuleID(cursor.getString(0));
            module.setModuleName(cursor.getString(1));
            module.setModuleTypeKey(cursor.getString(2));
            module.setModuleCode(cursor.getString(3));
            module_array.add(module);
        }

        cursor.close();


        return module_array;
    }


    /**
     * @param moduleID
     * @return 返回路线选型名称
     */
    private String getLuxianXuanxingName(String moduleID) {
        String luxianXuanxinName = "设置值";
        SQLiteDatabase db = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        assert db != null;
        @SuppressLint("Recycle") Cursor cursorm = db.rawQuery("select * from Module where moduleID=" + "'" + moduleID + "'", null);//此处的db是表名，name是列名
        while (cursorm.moveToNext()) {
            luxianXuanxinName = cursorm.getString(1);
        }
        cursorm.close();

        return luxianXuanxinName;
    }

    private List<ModuleType> getSubModuleTypeList(String parentKey) {
        List<ModuleType> mtlst = new ArrayList<>();
        SQLiteDatabase db = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        assert db != null;
        @SuppressLint("Recycle") Cursor cursorm = db.rawQuery("select * from ModuleType where ParentKey=" + "'" + parentKey + "'", null);//此处的db是表名，name是列名
        while (cursorm.moveToNext()) {
            ModuleType mt = new ModuleType();
            mt.setModuleTypeID(cursorm.getString(0));
            mt.setName(cursorm.getString(1));
            mt.setModuleTypeKey(cursorm.getString(2));
            mt.setParentKey(cursorm.getString(3));
            mtlst.add(mt);
        }
        allMT.addAll(mtlst);
        cursorm.close();


        for (ModuleType mt : mtlst) {
            getSubModuleTypeList(mt.getModuleTypeKey());
        }
        return allMT;
    }

    /**
     * @param
     * @return Equip
     */
    public List<ModuleType> getLiebie() {
        List<ModuleType> strings = null;
        try {
            strings = new ArrayList<>();
            SQLiteDatabase db = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
            assert db != null;
            @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from ModuleType mt where mt.ParentKey = '50000GT' and mt.State = '100'", null);//此处的db是表名，name是列名
            while (cursor.moveToNext()) {
                ModuleType equip = new ModuleType();
                equip.setModuleTypeID(cursor.getString(0));
                equip.setName(cursor.getString(1));
                equip.setModuleTypeKey(cursor.getString(2));
                strings.add(equip);
            }
            cursor.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return strings;
    }

    public List<Equip> getGuPiaoDai(int str, String wherestr) {
        List<Equip> strings = new ArrayList<>();
        SQLiteDatabase db = SQLiteDatabaseUtils.getInstance().getSQLiteDatabase();
        assert db != null;
        @SuppressLint("Recycle")
        Cursor cursor = db.rawQuery("select * from EquipType where component=" + str + " order by ID", null);//此处的db是表名，name是列名
        while (cursor.moveToNext()) {
            if (cursor.getString(1).contains(wherestr)) {
                Equip equip = new Equip();
                equip.setId(cursor.getInt(0));
                equip.setName(cursor.getString(1));
                equip.setCode(cursor.getString(4));
                strings.add(equip);
            }

        }
        cursor.close();

        return strings;
    }

    /**
     * 切换fragment
     *
     * @param my       当前fragment
     * @param fragment 需要切换的fragment
     */
    private void changeFragment(Fragment my, Fragment fragment) {
        assert my.getFragmentManager() != null;
        my.getFragmentManager()
                .beginTransaction()
                //.addToBackStack(null)  //将当前fragment加入到返回栈中
                .replace(R.id.de_content, fragment)
                .show(fragment)
                .commit();
    }
}
